package com.xyazm.pay.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.xyazm.common.enums.PayTypeEnum;
import com.xyazm.common.utils.ParamCheckUtil;
import com.xyazm.common.utils.RandomUtil;
import com.xyazm.pay.domain.dto.PayOrderDTO;
import com.xyazm.pay.domain.param.PayOrderParam;
import com.xyazm.pay.domain.param.PayRefundParam;
import com.xyazm.pay.service.IPayService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@Service
public class PayServiceImpl implements IPayService {

    @Override
    public PayOrderDTO payOrder(PayOrderParam param) {

        log.info("payOrder->request={}", JSONObject.toJSONString(param));
        String orderId = param.getOrderId();
        Integer payAmount = param.getPayAmount();
        String outTradeNo = RandomUtil.genRandomNumber(19);

        // 模拟调用了第三方支付平台的支付接口,后期补上

        // 组装返回数据
        PayOrderDTO payOrderDTO = new PayOrderDTO();
        payOrderDTO.setOrderId(orderId);
        payOrderDTO.setOutTradeNo(outTradeNo);
        payOrderDTO.setPayType(PayTypeEnum.WECHAT_PAY.getCode());

        Map<String, Object> payData = new HashMap<>();
        payData.put("appid", "wx207d34495e688e0c");
        payData.put("prepayId", RandomUtil.genRandomNumber(11));
        payData.put("payAmount", payAmount);
        payData.put("webUrl", "http://xxx/payurl");
        payOrderDTO.setPayData(payData);
        log.info("payOrder->response={}", JSONObject.toJSONString(payOrderDTO));

        return payOrderDTO;
    }

    @Override
    public void executeRefund(PayRefundParam param) {
        ParamCheckUtil.checkObjectNonNull(param);
        log.info("调用支付接口执行退款,订单号:{},售后单号:{}", param.getOrderId(), param.getAfterSaleId());
    }
}
